package org.amse.korovkin.ciphering.view;

import java.awt.event.ActionEvent;
import java.io.*;
import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;
import org.amse.korovkin.ciphering.Main;

public class MyJDialogActions {
    public static final String OPEN_SMALL_ICON_PATH = "org/amse/korovkin/ciphering/icons/open-16.png";
    public static final String CALCULATE_ICON_PATH = "org/amse/korovkin/ciphering/icons/calculate-16.png";

    public static class CancelAction extends AbstractAction {
        MyJDialog parent;
        public CancelAction(MyJDialog jFrame) {
            parent = jFrame;
            putValue(NAME, "Cancel");
            putValue(SHORT_DESCRIPTION, "Exit");
        }
        public void actionPerformed(ActionEvent e) {
            parent.pressCancel();
        }
    }
    public static class OkAction extends AbstractAction {
        MyJDialog parent;
        public OkAction(MyJDialog jFrame) {
            parent = jFrame;
            putValue(NAME, "OK");
            if (parent.getClass() == StatisticJDialog.class) {
                putValue(SHORT_DESCRIPTION, "Select StatisticFile");
            } else {
                putValue(SHORT_DESCRIPTION, "Select KeyFile");
            }
        }
        public void actionPerformed(ActionEvent e) {
            parent.pressOk();
        }
    }
    public static class OpenAction extends AbstractAction {
        MyJDialog parent;
        public OpenAction(MyJDialog jFrame) {
            parent = jFrame;
            ClassLoader cl = Main.class.getClassLoader();
            if (parent.getClass() == KeyJDialog.class) {
                putValue(NAME, "Open");
                putValue(SHORT_DESCRIPTION, "Open KeyFile");
                putValue(LARGE_ICON_KEY, new ImageIcon(cl.getResource(OPEN_SMALL_ICON_PATH)));
            } else {
                putValue(NAME, "New");
                putValue(SHORT_DESCRIPTION, "Choose File to calculate the statistic");
                putValue(LARGE_ICON_KEY, new ImageIcon(cl.getResource(CALCULATE_ICON_PATH)));
            }
        }
        public void actionPerformed(ActionEvent e) {
            JFileChooser fch;
            FileNameExtensionFilter filter;
            if (parent.getClass() == KeyJDialog.class) {
                filter = new FileNameExtensionFilter("KEY files", "key");
            } else {
                filter = new FileNameExtensionFilter("TXT files", "txt");
            }
            try {
                fch = new JFileChooser(MainJFrameActions.loadLastFilePath());
                fch.setFileFilter(filter);
            } catch (IOException ex) {
                ex.printStackTrace();
                fch = new JFileChooser(".");
            }
            int returnVal = fch.showOpenDialog(fch);
            if (returnVal == JFileChooser.APPROVE_OPTION) {
                parent.setFilePath(fch.getSelectedFile().getPath());
                if (parent.getClass() == StatisticJDialog.class) {
                    ((StatisticJDialog)parent).setLoadFlag(false);
                }
                try {
                    MainJFrameActions.saveLastFilePath(fch.getSelectedFile().getPath());
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
                if (parent.getClass() == StatisticJDialog.class) {
                    ((StatisticJDialog)parent).setLoadFlag(false);
                }
            }
        }
    }
    
}
